<?php
/**
 * Created by PhpStorm.
 * User: titarzhe
 * Date: 18-4-13
 * Time: 下午2:41
 */
namespace app\models;
use Phalcon\Validation;
/**
 * Trait CanValidateTrait
 * @property $this Model
 * @package app\models
 */
trait CanValidateTrait {

    abstract public function prepareValidation();

    public function validation()
    {
        $validation = $this->prepareValidation();
        return $this->validate($validation);
    }

    public function getRules($field)
    {
        $validators = [];
        $validation = $this->prepareValidation();
        foreach($validation->getValidators() as $validator){
            if($validator[0] == $field){
                $validators[] = $validator[1];
            }
        }
        return $validators;
    }
}